﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S9Engine;

public partial class Product : Parser
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try {
            _parser.SetCurrentDir("~/tpl");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);
            LoadFile("product_details.html", false);
            Session["CurrentPage"] = "Product.aspx";
            _parser.Assign("language" + Session["Language"].ToString().ToLower(), "class=\"current\"");
            LoadBreadCrumb(_cid, "BreadCrumb");
            LoadProductDetail(_pid);
            LoadOtherProduct();
           
            _parser.Assign("currentCategoryID", Request["cid"].ToString());
        }
        catch (Exception ex) {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadProductDetail(long ID)
    {
        if (ID == 0) { _parser.Parse("Main.Noresult"); return; }
               
        var tmpProductItem = LoadProductByID("Code", false, false, _pid).ToList();
        var productItem = tmpProductItem[0];
        if (tmpProductItem.Count == 0) { _parser.Parse("Main.Noresult"); return;}

        _parser.Assign("productID", productItem.ID.ToString());
        _parser.Assign("categoryID", productItem.Parent.ToString());
        _parser.Assign("productName", productItem.Name);
        _parser.Assign("productImage", productItem.Image);        
        _parser.Assign("productContent", productItem.Content);
        _parser.Assign("productExcerpt", productItem.Excerpt);
        _parser.Assign("productDate", productItem.Date.ToShortDateString());
        _parser.Assign("productCode", productItem.Meta.Select(m => m.Key, m => m.Value, "Code"));                        
        _parser.Assign("productSalePrice", productItem.Meta.Select(m => m.Key, m => m.Value, "SalePrice"));

        _parser.Parse("Main.ProductDetail");
    }
    private void LoadOtherProduct()
    {
        var products = LoadAllProducts("Code", false, false, 0).ToList();
        products = products.Where(p => !p.ID.Equals(_pid)).OrderBy(p=>p.Quantity).Take(10).ToList();
        if (products == null) {_parser.Parse("Main.NoOtherProduct"); return;}
        _parser.BeginUpdate("Main.OtherProduct");
        _parser.BeginUpdate("Main.OtherProduct.OtherProductItem");
        foreach (var pro in products) {
            _parser.Assign("productID", pro.ID.ToString());
            _parser.Assign("categoryID", pro.Parent.ToString());
            _parser.Assign("productName", pro.Name);
            _parser.Assign("productImage", pro.Image);
            _parser.Assign("productExcerpt", pro.Excerpt);
            _parser.Parse("Main.OtherProduct.OtherProductItem");
        }
        _parser.EndUpdate("Main.OtherProduct.OtherProductItem");
        if (products.Count > 0) _parser.Parse("Main.OtherProduct");
        _parser.EndUpdate("Main.OtherProduct");
    }
}